package com.social.model;

import java.util.Date;
import java.util.List;

/**
 * 好友关系模型类
 * 扩展支持好友推荐、共同好友计算等功能
 */
public class Friend {
    // 好友关系ID
    private int id;
    // 发起请求的用户ID
    private int userId;
    // 接收请求的用户ID
    private int friendUserId;
    // 好友请求状态
    private int status;
    // 共同好友数量
    private int mutualFriendCount;
    // 共同好友列表（实际应用中可能需要单独的实体类）
    private List<Integer> mutualFriendIds;
    // 请求发送时间
    private Date requestTime;
    // 请求处理时间
    private Date processTime;
    // 推荐理由（用于条件推荐）
    private String recommendReason;
    // 用户好友数量（用于条件推荐）
    private int friendCount;

    // 状态常量定义
    public static final int STATUS_PENDING = 0;   // 待处理
    public static final int STATUS_ACCEPTED = 1;  // 已接受
    public static final int STATUS_REJECTED = 2;  // 已拒绝
    public static final int STATUS_CANCELLED = 3; // 已取消

    /**
     * 无参构造方法
     */
    public Friend() {
    }

    /**
     * 带主要参数的构造方法
     * @param userId 发起请求的用户ID
     * @param friendUserId 接收请求的用户ID
     */
    public Friend(int userId, int friendUserId) {
        this.userId = userId;
        this.friendUserId = friendUserId;
        this.status = STATUS_PENDING;
        this.requestTime = new Date();
    }

    /**
     * 完整参数构造方法
     */
    public Friend(int id, int userId, int friendUserId, int status,
                  int mutualFriendCount, Date requestTime, Date processTime) {
        this.id = id;
        this.userId = userId;
        this.friendUserId = friendUserId;
        this.status = status;
        this.mutualFriendCount = mutualFriendCount;
        this.requestTime = requestTime;
        this.processTime = processTime;
    }

    // Getter和Setter方法
    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public int getUserId() {
        return userId;
    }

    public void setUserId(int userId) {
        this.userId = userId;
    }

    public int getFriendUserId() {
        return friendUserId;
    }

    public void setFriendUserId(int friendUserId) {
        this.friendUserId = friendUserId;
    }

    public int getStatus() {
        return status;
    }

    public void setStatus(int status) {
        this.status = status;
        if (status == STATUS_ACCEPTED || status == STATUS_REJECTED) {
            this.processTime = new Date();
        }
    }

    public int getMutualFriendCount() {
        return mutualFriendCount;
    }

    public void setMutualFriendCount(int mutualFriendCount) {
        this.mutualFriendCount = mutualFriendCount;
    }

    public List<Integer> getMutualFriendIds() {
        return mutualFriendIds;
    }

    public void setMutualFriendIds(List<Integer> mutualFriendIds) {
        this.mutualFriendIds = mutualFriendIds;
        if (mutualFriendIds != null) {
            this.mutualFriendCount = mutualFriendIds.size();
        }
    }

    public Date getRequestTime() {
        return requestTime;
    }

    public void setRequestTime(Date requestTime) {
        this.requestTime = requestTime;
    }

    public Date getProcessTime() {
        return processTime;
    }

    public void setProcessTime(Date processTime) {
        this.processTime = processTime;
    }

    public String getRecommendReason() {
        return recommendReason;
    }

    public void setRecommendReason(String recommendReason) {
        this.recommendReason = recommendReason;
    }

    public int getFriendCount() {
        return friendCount;
    }

    public void setFriendCount(int friendCount) {
        this.friendCount = friendCount;
    }

    /**
     * 发送好友请求
     * @return 是否发送成功
     */
    public boolean sendFriendRequest() {
        this.status = STATUS_PENDING;
        this.requestTime = new Date();
        return true;
    }

    /**
     * 接受好友请求
     * @return 是否接受成功
     */
    public boolean acceptFriendRequest() {
        if (this.status == STATUS_PENDING) {
            this.status = STATUS_ACCEPTED;
            this.processTime = new Date();
            return true;
        }
        return false;
    }

    /**
     * 拒绝好友请求
     * @return 是否拒绝成功
     */
    public boolean rejectFriendRequest() {
        if (this.status == STATUS_PENDING) {
            this.status = STATUS_REJECTED;
            this.processTime = new Date();
            return true;
        }
        return false;
    }

    /**
     * 取消好友请求
     * @return 是否取消成功
     */
    public boolean cancelFriendRequest() {
        if (this.status == STATUS_PENDING) {
            this.status = STATUS_CANCELLED;
            return true;
        }
        return false;
    }

    /**
     * 判断是否为好友关系
     * @return 是否为好友
     */
    public boolean isFriend() {
        return this.status == STATUS_ACCEPTED;
    }

    /**
     * 获取状态描述
     * @return 状态描述字符串
     */
    public String getStatusDescription() {
        switch (this.status) {
            case STATUS_PENDING:
                return "待处理";
            case STATUS_ACCEPTED:
                return "已接受";
            case STATUS_REJECTED:
                return "已拒绝";
            case STATUS_CANCELLED:
                return "已取消";
            default:
                return "未知状态";
        }
    }

    /**
     * 设置共同好友
     * @param mutualFriends 共同好友ID列表
     */
    public void setMutualFriends(List<Integer> mutualFriends) {
        this.mutualFriendIds = mutualFriends;
        this.mutualFriendCount = mutualFriends != null ? mutualFriends.size() : 0;
    }

    /**
     * 根据条件生成推荐理由
     * @param condition 推荐条件
     */
    public void generateRecommendReason(String condition) {
        switch (condition) {
            case "friendCountAsc":
                this.recommendReason = "好友数较少，容易建立联系";
                break;
            case "friendCountDesc":
                this.recommendReason = "好友数较多，社交活跃";
                break;
            case "sameCity":
                this.recommendReason = "与你同城，距离较近";
                break;
            case "sameSchool":
                this.recommendReason = "与你同校，校友关系";
                break;
            default:
                this.recommendReason = "系统推荐";
        }
    }

    @Override
    public String toString() {
        return "Friend{" +
                "id=" + id +
                ", userId=" + userId +
                ", friendUserId=" + friendUserId +
                ", status=" + status +
                ", mutualFriendCount=" + mutualFriendCount +
                ", requestTime=" + requestTime +
                ", processTime=" + processTime +
                ", recommendReason='" + recommendReason + '\'' +
                ", friendCount=" + friendCount +
                '}';
    }
}